package com.reshare.aspect;


import com.reshare.annotation.Log;
import com.reshare.base.ResultInfo;
import com.reshare.bean.SysLog;
import com.reshare.mapper.SysLogMapper;
import com.reshare.utils.HttpContextUtils;
import com.reshare.utils.IPUtils;
import com.reshare.utils.LoginUserUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/*功能描述
 * @author ahm
 */
@Aspect
@Component
public class LogAspect {

    @Autowired(required = false)
    private SysLogMapper sysLogMapper;

  // 切点
    @Pointcut("@annotation(com.reshare.annotation.Log)")
    public void pointcut() { }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point){
          Object result = null;
          long beginTime = System.currentTimeMillis();

        try {
            point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long time = System.currentTimeMillis() - beginTime;
        saveLog(point,time);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(200);
        resultInfo.setMsg("操作成功");
        return resultInfo;
    }

    private void saveLog(ProceedingJoinPoint point, long time) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = new SysLog();
        // 设置值
        Log logAnnotation = method.getAnnotation(Log.class);
        if (logAnnotation!=null){
            // 进行的操作
            sysLog.setOperation(logAnnotation.value());
        }
        // 请求的方法
        String className = point.getTarget().getClass().getName();
        // 方法名
        String methodName = signature.getName();
        sysLog.setOpMethod(className+"."+methodName+"()");
        // 请求的参数
        Object[] args = point.getArgs();
        // 参数名称
        LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer  = new LocalVariableTableParameterNameDiscoverer();
        String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
        if (args!=null && parameterNames!=null){
            // 遍历拼接参数
            String params = "";
            for (int i = 0; i <args.length ; i++) {
                params+= "  "+parameterNames[i]+":"+args[i];
            }
            // 设置参数
            sysLog.setOpParams(params);
            System.out.println("params="+params);
        }
        // 获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        // 设置IP 地址
        sysLog.setIp(IPUtils.getIpAddr(request));

        // 设置操作人
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        sysLog.setUserId(userId);
        sysLog.setOpDate(new Date());
        sysLog.setCreateTime(new Date());
        // 保存日志
        sysLogMapper.insertSelective(sysLog);
    }


}
